/**
 * 统计页面设备js
 * @author lh
 * 2016-01-15
 *
 */
/**
 * 设备数据表格属性配置
 */
//设置表格数据的样式。高度和宽度，刷新页面，样式重新调整
$('#medicalTechnician_eCharts').css('height',orange.mainPage.getHeight());
$('#medicalTechnician_eCharts').css('width',orange.mainPage.getWidth());
function medicalTechnician_query(){
	$('#medicalTechnician_Grid').datagrid({
		url:'s/techRes/table',
		method:'post',
		queryParams:{
			province:$("#mt_province").combobox('getText') == '全国' ? '': $("#mt_province").combobox('getText'),
			city:$("#mt_city").combobox('getText') == '全省' ? '': $("#mt_city").combobox('getText'),
			dist:$("#mt_district").combobox('getText') == '全市' ? '': $("#mt_district").combobox('getText')
		},
		loadFilter:orange.systemPage.selfLoadFilter
	});
	initTechnicianChartData();
}
	$('#medicalTechnician_Grid').datagrid({ 
		//url:'s/techRes/table',
	    singleSelect:true,
	    rownumbers:true,
	    columns:[[ 
	        {field:'guid',hidden:true},
	        {field:'orgName',title:'中医馆名',width:'25%',sortable:true,halign:'center',formatter:mt_checkDetail},
	        {field:'techTotalc',title:'技师总数',width:'25%',sortable:true,halign:'center'}, 
	        {field:'techDiagc',title:'诊断技师',width:'25%',sortable:true,halign:'center'},
	        {field:'techReco',title:'康复理疗技师',width:'23%',sortable:true,halign:'center',formatter:mt_option}
	    ]],
	    pagination:true,
	    sortable:true,
	    sortName: 'type',
	    sortOrder: 'asc'
	});
	//康复理疗技师，未找到相应字段，用“技师总数”-“诊断技师”定义为“康复理疗技师”
	function mt_option(value,rows){
		return (rows.techTotalc-rows.techDiagc);
	}
	//grid link
	function mt_checkDetail(value, row, index){
		if(value==null){
			return null;
		}else{
			return '<a href="#" onclick="tcmReloadOrCreate(\''+row.guid+'\',\''+row.orgName+'\')">'+value+'</a>';	
		}
	};
	//查询条件combobox配置
	//省级数据
	var clearData=[];//清空数据
	var pData=[{'id':1,'text':'全国'},{'id':2,'text':'北京市'},{'id':3,'text':'天津市'},{'id':4,'text':'河北省'}];
	$("#mt_province").combobox({
		data:pData,   
	    valueField:'id',   
	    textField:'text',
	    onLoadSuccess:function(data){
	    	$("#mt_province").combobox('setValue',data[0].id);
	    },
	    onSelect:function(node){
	    	if(node.id==1){
	    		$("#mt_city").combobox({data:clearData});
	    		$("#mt_district").combobox({data:clearData});
	    	}else{
	    		mt_city();//$("#mt_city + .combo").show();
	    	}
	    }
	});
	var cData=[{'id':1,'text':'全省'},{'id':2,'text':'北京市'},{'id':3,'text':'天津市'},{'id':4,'text':'石家庄市'}];
	$("#mt_city").combobox({
		data:cData,   
	    valueField:'id',   
	    textField:'text',
	    onLoadSuccess:function(data){
	    },
	    onSelect:function(node){
	    	if(node.id==1){
	    		$("#mt_district").combobox({data:clearData});
	    	}else{
	    		mt_district();//$("#mt_town + .combo").show();
	    	}
	    } 
	});
	var tData=[{'id':1,'text':'全市'},{'id':2,'text':'新华区'},{'id':3,'text':'朝阳区'},{'id':4,'text':'西城区'}];
	$("#mt_district").combobox({
		data:tData,   
	    valueField:'id',   
	    textField:'text' ,
	    onLoadSuccess:function(data){
	    }
	});
	//市级数据
	function mt_city(){
		$("#mt_city").combobox({data:cData});
	}
	//区域数据
	function mt_district(){
		$("#mt_district").combobox({data:tData});
	}
	//echarts图表配置
	//页面初始化加载
	var myChart = echarts.init($("#medicalTechnician_eCharts")[0]);
	var initData;
	var seriesData1=[];//纵轴数据
	var seriesData2=[];//纵轴数据
	var xData=[];//横轴数据
	var seriesData3=[];//纵轴数据
	/**
	 * 初始化技师chart 数据
	 */
	initTechnicianChartData();
	function initTechnicianChartData(){
		debugger;
		seriesData1.length = 0;
		seriesData2.length = 0;
		xData.length = 0;
		seriesData3.length = 0;
		$.ajax({
			url:'s/chartTechRes',
			type: 'post',
			data:{
				province:$("#mt_province").combobox('getText') == '全国' ? '': $("#mt_province").combobox('getText'),
				city:$("#mt_city").combobox('getText') == '全省' ? '': $("#mt_city").combobox('getText'),
				dist:$("#mt_district").combobox('getText') == '全市' ? '': $("#mt_district").combobox('getText')
			},
	        success: function(data) {
	        	initData = JSON.parse(data)
	        	//ajax请求，加载数据
	        	for (var i=0;i<initData.length;i++){
	        		xData.push(initData[i].period);
	        		seriesData1.push(initData[i].tcm_technician_sum);
	        		seriesData2.push(initData[i].tcm_technician_diagnosis);
	        		seriesData3.push(initData[i].tcm_technician_physiotherapy);
	        	}
	        	mt_eChart_option(xData,seriesData1,seriesData2,seriesData3,myChart);
	        }
	    });
	}
    // 指定图表的配置项和数据
	function mt_eChart_option(xData,seriesData1,seriesData2,seriesData3,myChart){
		option = {
				grid:{
					show:false,
					bottom :'15%',
					top:'25%',
					height:'65%',
					width:'85%',
					left:'7%',
					right:'8%'
				},
			    tooltip : {
			        trigger: 'axis'
			    },
			    legend: {
			        data:['技师总数','诊断技师数量','康复理疗技师数量'],//对应serias的顺序，保持一致
			        top:'8%'
			    },
			    calculable : true,
			    xAxis : [
			        {
			            type : 'category',
			            boundaryGap : false, 
			            data : xData//['2013-04','2013-07','2013-10','2014-01','2014-07','2014-10','2015-01','2015-04','2015-07','2015-10','2016-01']
			        }
			    ],
			    yAxis : [
			        {
			            type : 'value'
			        }
			    ],
			    series : [
			        {
			            name:'技师总数',
			            type:'line',
			            data:seriesData1
			        },
			        {
			            name:'诊断技师数量',
			            type:'line',
			            data:seriesData2//[25,41,53,76,88,103,124,137,152,163,169,168]
			        },
			        {
			            name:'康复理疗技师数量',
			            type:'line',
			            stack: '总量',
			            data:seriesData3
			        }
			    ]
			};
		        // 使用刚指定的配置项和数据显示图表。
		    myChart.setOption(option);
	}
function mt_reLoadOption(){
	$.ajax({
//        url:'',
        type: 'post',
        data:{
        	province:$("#mt_province").combobox('getText') == '全国' ? '': $("#mt_province").combobox('getText'),
			city:$("#mt_city").combobox('getText') == '全省' ? '': $("#mt_city").combobox('getText'),
			dist:$("#mt_district").combobox('getText') == '全市' ? '': $("#mt_district").combobox('getText')
		},
        success: function(data) {
        	var initData=eval(data);
        	var seriesData1=[];//纵轴数据
        	var seriesData2=[];//纵轴数据
        	var seriesData3=[];//纵轴数据
        	var xData=[];//横轴数据
        	var myChart = echarts.init($("#medicalTechnician_eCharts")[0]);
        	for (var i=0;i<initData.length;i++){
        		xData.push(initData[i].period);
        		seriesData1.push(initData[i].techTotal);
        		seriesData2.push(initData[i].techBran01);
        		seriesData3.push(initData[i].techBran02);
        	}
        	mt_eChart_option(xData,seriesData1,seriesData2,seriesData3,myChart);
        }
    });
}